package adrian.sunlight.jsp;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author adrian
 */
public class RemoteEventLoggerServer extends Thread{

    private ServerSocket Listener;
    private Socket       Remote;

    protected String	 END_COMMUNICATION_NOTIFIER = "COMMUNICATION_END";
    
    private volatile boolean STOP 		 		= false;
    private volatile boolean WAS_STOPPED_BY_USER = false;
    
    private BufferedReader Input;
    public 	String[] 	   Buffer;
    private static int 	   MAX_BUFFER_SIZE = 128;
    public  int 		   BUFFER_SIZE;
    public  int			   BUFFER_POS;
    
    public int      port;
 
    
    public RemoteEventLoggerServer() throws IOException{
    	this.BUFFER_SIZE = 64;
    	this.BUFFER_POS  = 0;
        this.Buffer   	 = new String[this.BUFFER_SIZE];
        this.port		 = 1987;
        this.Listener 	 = new ServerSocket(this.port);
    }

    public RemoteEventLoggerServer(int port) throws IOException{
        this.Buffer   	 = new String[64];
        this.BUFFER_POS  = 0;
        this.port		 = port;
        this.Listener 	 = new ServerSocket(port, 0, InetAddress.getLocalHost());
    }

    public RemoteEventLoggerServer(int port, int bufferSize) throws IOException{
    	this.BUFFER_SIZE = bufferSize > RemoteEventLoggerServer.MAX_BUFFER_SIZE ? RemoteEventLoggerServer.MAX_BUFFER_SIZE : bufferSize;
    	this.BUFFER_POS  = 0;
        this.Buffer   	 = new String[bufferSize];
        this.port		 = port;
        this.Listener 	 = new ServerSocket(port, 0, InetAddress.getLocalHost());
    }

    public void open(){
    	this.STOP = false;
    	
    	if(this.Listener.isClosed()){
    		try {
				this.Listener = new ServerSocket(this.port);
			} catch (IOException e) {
				this.Buffer[this.BUFFER_POS] = e.toString();
			}
    	}
    	
    	System.out.println("stop=false");
    }
    
    @Override
    public void run(){
        System.out.println("Serverul de ascultare jurnale a fost pornit pe port-ul " + this.Listener.getLocalPort() + " la adresa " + this.Listener.getInetAddress().toString());
        this.Buffer[0] = "Serverul de ascultare jurnale a fost pornit pe port-ul " + this.Listener.getLocalPort() + " la adresa " + this.Listener.getInetAddress().toString();
        
        try {
            this.Remote = this.Listener.accept();
            this.Input  = new BufferedReader(new InputStreamReader(this.Remote.getInputStream()));
            
            this.addToBuffer("Conexiune de la: " + this.Remote.getInetAddress().toString());

            while(this.Remote.isConnected()){
                String line = this.Input.readLine();
                
                if(line == null){
                    continue;
                }
                if(line.equals(this.END_COMMUNICATION_NOTIFIER)){
                    break;
                }
                else{
                  if(this.BUFFER_POS == this.BUFFER_SIZE){
                      this.pushInBuffer(line);
                  }
                  else{
                      this.addToBuffer(line);
                  }
               }
                
                if(this.STOP){
                	break;
                }
            }
                 
         } catch (IOException ex) {
        	 this.Buffer[this.BUFFER_POS] = ex.toString();
         }
         
         System.out.println("AM oprit serverul!");
         this.Buffer[this.BUFFER_POS] = "Serverul este oprit.";
    }


    public void pushInBuffer(String message){
        int top = this.Buffer.length -1;

        for(int i=0; i<top; i++){
            this.Buffer[i] = this.Buffer[i+1];
        }

        this.Buffer[top] = message;
        this.BUFFER_POS	 = top;
    }

    public void addToBuffer(String message){
    	this.BUFFER_POS++;
        this.Buffer[this.BUFFER_POS] = message;
    }

    public String getLastMessage(){
        return this.Buffer[this.BUFFER_POS];
    }
    
    public String getLastMessages(int num){
        if(num > this.BUFFER_POS){
        	num = this.BUFFER_POS;
        }
        
        String msgs = "";
        
        for(int i=num; i>=0; i--){
        	msgs += msgs.length() > 0 ? ("|" + this.Buffer[i]) : this.Buffer[i];
        }
        
        return msgs;
    }

    public void close(){
    	
    	this.BUFFER_POS = 0;
    	this.Buffer		= null;
    	this.Buffer		= new String[this.BUFFER_SIZE];
    	
        try {
        	
        	if(this.Listener.isBound()){
        		this.Listener.close();
        	}
        	
            this.Remote.close();
        } catch (IOException ex) {
        	this.Buffer[this.BUFFER_POS] = ex.toString();
        }
        catch (NullPointerException ne) {
        	this.Buffer[this.BUFFER_POS] = ne.toString();
         }
        
        this.STOP 				 = true;
        this.WAS_STOPPED_BY_USER = true;
    }
    
    public boolean isStoppedByUser(){
    	return this.WAS_STOPPED_BY_USER;
    }
}
